<template>
	<view class="">
		<view class="logoTitle">
			<view class="textCenter" style="padding-top: 30px;">
				<view class="logoImg">
					<image src="../../static/logohong.png" mode=""></image>
				</view>
			</view>
			<view class="textCenter" style="font-size: 24px;margin-top: 30px;">
				工地云课堂
			</view>

			<view class="textCenter" style="margin-top: 20px;color: #666666;font-size: 14px;">
				第二步：绑定手机号
			</view>


		</view>
		<view style="width: 100%;margin: 0 auto;text-align: center;">
			<van-button round type="primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">微信用户一键绑定</van-button>

			<!-- 	<view style=" margin:30px auto;width: 80%;text-align: center;font-size: 14px;color:#485AFF;">
				仅限开通项目用户使用
			</view> -->
		</view>
		<van-toast id="van-toast" />
	</view>
</template>

<script>
	import Toast from '@/wxcomponents/vant-weapp/toast/toast';
	import WXBizDataCrypt from "@/wxcomponents/WXBizDataCrypt.js";
	import {
		bindUser,
		wxLogin,
		getWxUserInfo
	} from "../../api/login/index.js"
	export default {
		data() {
			return {
				phoneNum: '',
				userName: ''
			}
		},
		onLoad(option) {
			this.userName = option.username
		},
		methods: {
			onGetPhoneNumber(PhoneNumber) {
				let appid = "wx5175026ddf41f0ed" //需替换
				let secret = "f9339ef4928de17137c97dcbce07a90d" //需替换
				//调用 wx.login 接口,获取code
				uni.login({
					provider: 'weixin',

					success: res => {
						console.log(11111111)
						console.log(uni.getStorageSync('unionOpen'));
						//解密用户信息
						let pc = new WXBizDataCrypt(appid, uni.getStorageSync('unionOpen').session_key);
						// console.log(pc);
						// console.log(PhoneNumber);
						
						let data = pc.decryptData(PhoneNumber.detail.encryptedData, PhoneNumber.detail.iv);

						let parameter = {
							"bindId": uni.getStorageSync('realName').id,
							"cellphone": data.purePhoneNumber,
							"fullName": uni.getStorageSync('username'),
							"idNo": uni.getStorageSync('realName').idNo,
							"nickName": uni.getStorageSync('userInfo').nickName,
							"openId": uni.getStorageSync('unionOpen').openid,
							"unionId": uni.getStorageSync('unionOpen').unionid,
						}
						console.log(parameter);
						bindUser(parameter).then(res => {
							// uni.showToast({
							// 	title: '绑定成功',
							// 	duration: 1000
							// })
							if (res.data.code != 200) {
								Toast.fail(res.data.message);
							} else {
								Toast.success('绑定成功');
								console.log(111111111);
								let spaceId = ''
								wxLogin({
									unionId: uni.getStorageSync('unionOpen').unionid
								}).then(res => {
									// console.log(res);
									uni.setStorageSync('spaceData', res.data.data.appSpaceVOList[0])
									uni.setStorageSync('appSpaceVOList', res.data.data.appSpaceVOList)
									uni.redirectTo({
										url: `../home/home?id=${res.data.data.id}`
									});
								})

							}

						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.logoTitle {
		text-align: center;
		margin-bottom: 50px;
	}

	.textCenter {
		width: 200px;
		margin: 0 auto;
	}

	.logoImg {
		height: 85px;
		width: 85px;
		margin: 0 auto;
		/* background-color: #7D7E80; */
	}

	::v-deep .van-button--primary {

		width: 80%;

	}

	::v-deep image {
		width: 100%;
		height: 100%;
	}

	::v-deep .van-field__input {
		margin-left: 12px;
	}
</style>
